package com.woorigil.social.domain;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woorigil.social.Context;
import com.woorigil.social.MenuCategory;
import com.woorigil.social.MobileAppType;
import com.woorigil.social.domain.app.MobileApp;
import com.woorigil.social.domain.app.MobileAppDao;
import com.woorigil.social.domain.category.Category;
import com.woorigil.social.domain.category.CategoryCondition;
import com.woorigil.social.domain.category.CategoryDao;
import com.woorigil.social.domain.deal.DealCondition;
import com.woorigil.social.domain.deal.DealDao;
import com.woorigil.social.domain.history.WorkHistory;
import com.woorigil.social.domain.history.WorkHistoryDao;
import com.woorigil.social.domain.provider.Provider;
import com.woorigil.social.domain.provider.ProviderDao;
import com.woorigil.social.worker.DealDataLoadWorker;

@Service
public class ApplicationService {
	@Autowired DealDao dealDao;
	@Autowired CategoryDao categoryDao;
	@Autowired WorkHistoryDao workHistoryDao;
	@Autowired MobileAppDao mobileAppDao;
	@Autowired ProviderDao providerDao;
	
	public void menuInitialize() {
		WorkHistory workHistory = workHistoryDao.selectLastWorkDate(DealDataLoadWorker.WORK_NAME);
		Context.setLastUpdatedAt((workHistory == null)? null : workHistory.getWorkDate());
		Context.setTotalDealCount(dealDao.selectTotalCount(new DealCondition()));
		Context.setMainDeals(null);
		settingMenusAndCategories(categoryDao.search(new CategoryCondition()));
	}
	
	public void initializeCategory() {
		List<Category> allCategories = categoryDao.search(new CategoryCondition());
		Context.allCategories = categoryDao.searchAll();
		for (Category each : allCategories) {
			if (each.getLevelNo() == 2) {
				Context.addCategory(each.getName(), each);
			}
		}
	}
	
	private void settingMenusAndCategories(List<Category> allCategories) {
		//main menu set
		Context.getMainMenus().clear();
		Map<Integer, MenuCategory> mainMenus = new HashMap<Integer, MenuCategory>();
		for (Category each : allCategories) {
			if (each.getLevelNo() == 1) {
				MenuCategory mainMenu = mainMenus.get(each.getId());
				if (mainMenu == null) {
					mainMenu = new MenuCategory();
				}
				mainMenu.setCategory(each);
				mainMenus.put(each.getId(), mainMenu);
				Context.addMainMenu(mainMenu);
			}
		}
		
		//sub category set
		for (Category each : allCategories) {
			if (each.getLevelNo() == 2) {
				MenuCategory mainMenu = mainMenus.get(each.getParentId());
				mainMenu.addSubCategory(each);
			}
		}
		
		//deal count set
		for (Integer each : mainMenus.keySet()) {
			MenuCategory mainMenu = mainMenus.get(each);
			DealCondition condition = new DealCondition();
			condition.setCategoryIds(mainMenu.getSubCategoryIds());
			mainMenu.setDealCount(dealDao.selectTotalCount(condition));
		}
	}

	public MobileApp getLastReleaseAppVersion(MobileAppType mobileApp) {
		return mobileAppDao.selectByName(mobileApp.name());
	}
	
	public MobileApp getLastReleaseDataVersion(MobileAppType mobileApp) {
		return mobileAppDao.selectByName(mobileApp.name());
	}

	public int getTotalItemCount() {
		return Context.getTotalDealCount();
	}

	public Date getLastUpdatedAt() {
		return Context.getLastUpdatedAt();
	}

	public List<Category> getCategories() {
		return Context.allCategories;
	}

	public List<Provider> getProviders() {
		return providerDao.selectList(true);
	}
}
